package acom.test.swing.bb;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;

public class PeoplePanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private JLabel title;
	private JList list;
	private JButton add_but;
	private JButton del_but;
	private JScrollPane scrollPane;
	private JFileChooser fileChooser;

	private PeopleModelList mode;

	private int pePan_x = 20;
	private int pePan_y = 20;

	private List<AnswerPeople> peoples = new ArrayList<AnswerPeople>();

	public PeoplePanel() {
		this.setLayout(null);
		title = new JLabel("这是候选人名单");
		mode = new PeopleModelList();
		list = new JList(mode);

		list.setLayoutOrientation(JList.VERTICAL_WRAP);
		list.setVisibleRowCount(9);
		
		int listH = list.getHeight();
		int listW = list.getWidth();
		System.out.println("listH===>"+listH);
		System.out.println("listW===>"+listW);

		fileChooser = new JFileChooser("D:\\");

		scrollPane = new JScrollPane(list);
		//scrollPane.setPreferredSize(new Dimension(350, 300));
		

		ImageIcon add_icon = SystemUtil.createImageIcon("add_16.png");
		ImageIcon del_icon = SystemUtil.createImageIcon("remove_16.png");

		add_but = new JButton(add_icon);
		add_but.setText("ab");
		del_but = new JButton(del_icon);

		init();

		this.add(title);
		this.add(scrollPane);
		this.add(add_but);
		this.add(del_but);
	}

	public void init() {
		title.setBounds(pePan_x, pePan_y, 120, 20);
		scrollPane.setBounds(pePan_x, pePan_y + 30, 220, 150);
		add_but.setBounds(pePan_x + 240, pePan_y + 30, 60, 50);
		del_but.setBounds(pePan_x + 240, pePan_y + 70, 25, 25);

		add_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 导入抽奖人员数据源
				DataSource_Action(e);
			}
		});

		del_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 删除抽奖人员
				delete_Action(e);
			}
		});
	}

	public void delete_Action(MouseEvent e) {

		int[] index = list.getSelectedIndices();
		int endindex = index.length - 1;
		for (int i = endindex; i >= 0; i--) {
			System.out.println("选中的====" + index[i]);
			mode.removeElementAt(index[i]);
			peoples.remove(index[i]);
		}

	}

	protected void DataSource_Action(MouseEvent e) {
		fileChooser.setApproveButtonText("确定");
		fileChooser.setDialogTitle("打开文件");
		File file = null;
		int result = fileChooser.showOpenDialog(this);

		if (result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			System.out.println("您导入的文件：" + file.getName());
		} else if (result == JFileChooser.CANCEL_OPTION) {
			System.out.println("您没有导入任何文件");
		}

		if (file != null) {
			List<AnswerPeople> addpeoples = ExcelUtil.readExcelFile(file);
			peoples.addAll(addpeoples);
			SystemUtil.setPeoplelist(peoples);

			if (peoples.size() > 1) {
				mode = new PeopleModelList();
				list.setModel(mode);
				int listH = list.getHeight();
				int listW = list.getWidth();
				System.out.println("888listH===>"+listH);
				System.out.println("888listW===>"+listW);
				list.setFont(new Font("楷体",Font.PLAIN,26));
			} else {
				System.out.println("没有读取到excel的内容");
			}
		}
	}

	class PeopleModelList extends DefaultListModel// 创建一个继承了DefaultListModel类的类
	{
		private static final long serialVersionUID = 1L;

		PeopleModelList() {
			for (int i = 0; i < peoples.size(); i++)
				addElement((i + 1) + "." + peoples.get(i).getPeopleName());
		}

	}

}
